﻿using System.Collections.Generic;
using Apache.Cassandra;
using Thrift.Protocol;
using Thrift.Transport;

namespace SKBKontur.Cassandra.Proxy
{
    public class Configurator : IConfigurator
    {
        private readonly TTransport transport;
        private readonly Apache.Cassandra.Cassandra.Client client;

        public Configurator(TTransport transport)
        {
            this.transport = transport;
            var protocol = new TBinaryProtocol(transport);
            client = new Apache.Cassandra.Cassandra.Client(protocol);
        }

        #region IConfigurator Members

        public void Dispose()
        {
            transport.Close();
        }

        public void Truncate(string keySpace, params CfDef[] columnFamilies)
        {
            client.set_keyspace(keySpace);
            foreach (var columnFamily in columnFamilies)
                client.truncate(columnFamily.Name);
        }

        public void DropKeyspace(string name)
        {
            client.system_drop_keyspace(name);
        }

        public void CreateKeyspace(KsDef keyspace)
        {
            client.system_add_keyspace(keyspace);
        }

        public void UpdateColumnFamily(CfDef cfDef) // todo проверить логику установки кейспейса
        {
            client.set_keyspace(cfDef.Keyspace);
            client.system_update_column_family(cfDef);
        }

        public List<KsDef> Keyspaces()
        {
            return client.describe_keyspaces();
        }

        public void AddColumnFamily(CfDef cfDef)
        {
            client.set_keyspace(cfDef.Keyspace);
            client.system_add_column_family(cfDef);
        }
        #endregion
    }
}